package com.cskaoyan.gateway.controller.shopping;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.mall.commons.result.ResponseData;
import com.mall.commons.result.ResponseUtil;
import com.mall.commons.tool.utils.CookieUtil;
import com.mall.shopping.ICartService;
import com.mall.shopping.constants.ShoppingRetCode;
import com.mall.shopping.dto.*;
import com.mall.user.annotation.Anoymous;
import com.mall.user.intercepter.TokenIntercepter;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @description:
 * @author: 牛犇
 * @date 2021/12/13 15:58
 */
@RestController
@RequestMapping("/shopping")
public class CartsController {
    @Reference(check = false)
    ICartService iCartService;


    @GetMapping("carts")
    public ResponseData cartList(HttpServletRequest request){
        //获得用户id
        String userInfo = (String) request.getAttribute(TokenIntercepter.USER_INFO_KEY);
        JSONObject object = JSON.parseObject(userInfo);
        Long uid = Long.parseLong(object.get("uid").toString());
        CartListByIdRequest cartListByIdRequest=new CartListByIdRequest();
        cartListByIdRequest.setUserId(uid);
        //获取购物车列表集合
        List<CartProductDto> cartListById = iCartService.getCartListById(cartListByIdRequest);
        return new ResponseUtil<>().setData(cartListById,"success");
    }

    @PostMapping("carts")
    public ResponseData addCarts(@RequestBody AddCartRequest request){
        AddCartResponse addCartResponse = iCartService.addToCart(request);
        if (!addCartResponse.getCode().equals(ShoppingRetCode.SUCCESS.getCode()))
        {
            return new ResponseUtil().setErrorMsg(addCartResponse.getMsg());
        }
        return  new ResponseUtil<>().setData("成功");
    }

    @PutMapping("carts")
    public ResponseData updateCarts(@RequestBody UpdateCartNumRequest request){
        iCartService.updateCartNum(request);
        return  new ResponseUtil<>().setData("成功");
    }

    @DeleteMapping("carts/{uid}/{pid}")
    public ResponseData deleteCarts(@PathVariable("uid") Long uid,@PathVariable("pid") Long pid){
        DeleteCartItemRequest request = new DeleteCartItemRequest();
        request.setUserId(uid);
        request.setItemId(pid);
        iCartService.deleteCartItem(request);
        return  new ResponseUtil<>().setData("成功");
    }

    /**
     * @Author ChenZhuqi
     * @Date 2021/12/13 15:43
     * @Description： 删除选中购物车
     */
    @DeleteMapping("/items/{uid}")
    public ResponseData itemsUidByChen(@PathVariable("uid") Long uid) {
        DeleteCheckedItemRequest request = new DeleteCheckedItemRequest();
        request.setUserId(uid);
        DeleteCheckedItemResposne response = iCartService.deleteCheckedItem(request);
        if (response.getCode().equals(ShoppingRetCode.SUCCESS.getCode())) {

            return new ResponseUtil().setData("成功");
        }
        return new ResponseUtil().setErrorMsg(response.getMsg());
    }

    /**
     * @Author ChenZhuqi
     * @Date 2021/12/13 15:43
     * @Description： 全选购物车
     */
    @PutMapping("/items")
    public ResponseData itemsAllByChen(@RequestBody CheckAllItemRequest request) {

        CheckAllItemResponse response = iCartService.checkAllCartItem(request);
        if (response.getCode().equals(ShoppingRetCode.SUCCESS.getCode())) {

            return new ResponseUtil().setData("成功");
        }
        return new ResponseUtil().setErrorMsg(response.getMsg());


    }

}
